// pages/Login/login.js
const app = getApp().$app;
import Taro from '@tarojs/taro'
import MyUtils from '../noticeApp/components/personal/utils';


Page({
  /**
   * 页面的初始数据
   */
  data: {
    loginParams: app.loginParams
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

    console.log("登录页面设置参数到appData中")
    console.log(options);
    app.goToPath = options;
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {},

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {},

  loginSucceed: function (sender) {
    console.log(sender);
    var data = sender.detail;

    this.loginToMainPageWithData(data);
  },



  //获取数据成功，登录到对应页面
  loginToMainPageWithData: function (data) {
    var that = this;
    console.log(data);
    app.globalData.loginInfo = data.data;
    app.globalData.userInfo = data.data.userInfo;
    app.globalData.userName = data.data.userInfo.UserName;
    app.globalData.Token = data.data.userInfo.Token;
    app.globalData.schoolID = data.data.schoolInfo.SchoolID;
    app.globalData.groupID = data.data.userInfo.GroupID;
    app.globalData.gender = data.data.userInfo.Gender;
    app.globalData.userID = data.data.userInfo.UserID;
    app.globalData.userType = app.globalData.loginInfo.userInfo.UserType;
    app.globalData.schoolUrl = data.data.schoolInfo.HostServerUrl;
    app.globalData.schoolLevel = data.data.schoolInfo.SchoolLevel;
    app.globalData.Psw = data.data.userInfo.Psw;
    app.globalData.localuserInfo = data.data.userInfo;
    app.globalData.isLogin = true;
    app.globalData.unionID = data.data.wxsession.unionid;
    // app.globalData.openID = "";
    // data.data.wxsession.openid = "";
    // console.log("___________登陆成功___________");
    app.globalData.openID
      ? ""
      : (app.globalData.openID =
          data.data.wxsession.openid || that.getStorageOpenIDAndSetApp());
    // app.Token(),
    app.refreshToken();
    //生成映射地址的头像
    let exec = /\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}/g, //匹配IP正则
      mapAddr = app.globalData.schoolUrl.match(exec), //183.63.90.221
      insidePhothoUrl = data.data.userInfo.PhotoPath; // http://192.168.122.100:20103/http_basic/UserInfo/Avatar/Default/Nopic111.jpg
    mapAddr = (mapAddr && mapAddr[0]) || "";
    app.globalData.mapExec = exec;
    app.globalData.schoolIP = mapAddr;
    app.globalData.userPhotoUrl = insidePhothoUrl.replace(exec, mapAddr);
    //家长身份进入realUserID采用学生账号调用接口
    if (app.globalData.userType == "3") {
      app.globalData.realUserID = app.globalData.userID.substr(3);
    } else {
      app.globalData.realUserID = app.globalData.userID;
    }
    that.redirectToRolePage();
  },
  // 获取缓存中的OpendID并且设置到App中
  getStorageOpenIDAndSetApp: function () {
    wx.getStorage({
      key: "openID",
      success: (res) => {
        console.log("--------------->获取缓存中的openID<------------------");
        if (!app.globalData.openID) {
          app.globalData.openID = res.data ? res.data : null;
          console.log(
            "--------------->设置openID到App中的openID<------------------"
          );
        }
        console.log(app);
      },
    });
  },
  //跳转到对应界面
  redirectToRolePage: function () {
    var that = this;
    var curRole = 2;
    // var userClassStr = app.globalData.loginInfo.userInfo.UserClass;

    that.setData({
      wxLoginInfo: app.globalData.loginInfo,
      currentRole: curRole,
    });
    app.showToastError("登录成功");
    console.log("小程序app存储",app.globalData)
    
    let referrerInfo = Taro.getEnterOptionsSync().referrerInfo
    console.log("referrerInfo",referrerInfo)
    let lg_ic = referrerInfo?.extraData?.lg_ic || ""
    MyUtils.setStorage("lg_ic",lg_ic)

    if(app.globalData.userType == 2 || app.globalData.userType == 3){//非学生和家长才可以进
      Taro.showToast({
        title:"无权限",
        icon:'none',
        duration:2000
      })
    }else{
      Taro.request({
        url:'https://campus.lancooedu.com/school/getSubsystemBySystemId',
        method:'GET',
        data:{
          schoolId:app.globalData.schoolID,
          systemId:'220'
        },
        success(res){
          console.log("API-子系统地址:",res)
          if(!res.data.data.url){
            console.log("无子系统地址")
            Taro.showToast({
              title:"子系统地址为空",
              icon:"none",
              duration:2000
            })
          }else{
            MyUtils.setStorage("baseUrl220",res.data.data.url)
            Taro.redirectTo({
              url:"/pages/noticeApp/index/index"
            })
          }
        }
      })
      // MyUtils.setStorage("baseUrl220", "http://120.196.79.139:22101/notice")
      // Taro.redirectTo({
      //   url:"/pages/noticeApp/index/index"
      // })
    }
    // Taro.navigateTo({
    //   url:'/pages/GZ001A/index'//跳转成功后的地址
    // })
  },
});
